part of '../widgets.dart';

/// [VanCellGroup]属性常量
const kVanCellGroupBackground = VanCssKey._raw('--van-cell-group-background');
const kVanCellGroupTitleColor = VanCssKey._raw('--van-cell-group-title-color');
const kVanCellGroupTitlePadding = VanCssKey._raw('--van-cell-group-title-padding');
const kVanCellGroupTitleFontSize = VanCssKey._raw('--van-cell-group-title-font-size');
const kVanCellGroupTitleLineHeight = VanCssKey._raw('--van-cell-group-title-line-height');
const kVanCellGroupInsetPadding = VanCssKey._raw('--van-cell-group-inset-padding');
const kVanCellGroupInsetRadius = VanCssKey._raw('--van-cell-group-inset-radius');
const kVanCellGroupInsetTitlePadding = VanCssKey._raw('--van-cell-group-inset-title-padding');

/// [VanCellGroup]主题类
@immutable
class VanCellGroupTheme extends VanProvider<VanCellGroupThemeData> with VanProvide {
  const VanCellGroupTheme({super.key, required super.child, required super.provide});

  static VanCellGroupThemeData of(BuildContext context) => VanProvider.of<VanCellGroupTheme>(context)?.provide ?? VanTheme.of(context).cellGroupTheme;
  static const _prefix = "--van-cell-group-";

  static bool support(VanCssKey vanCssKey) => vanCssKey.key.startsWith(_prefix);

  static dynamic get<T>(BuildContext context, {required VanCssKey key, T? defaultValue}) {
    final VanCellGroupThemeData cellGroupTheme = VanCellGroupTheme.of(context);
    if (kVanCellGroupBackground == key) return cellGroupTheme.background.get(context);
    if (kVanCellGroupTitleColor == key) return cellGroupTheme.titleColor.get(context);
    if (kVanCellGroupTitlePadding == key) return cellGroupTheme.titlePadding.get(context);
    if (kVanCellGroupTitleFontSize == key) return cellGroupTheme.titleFontSize.get(context);
    if (kVanCellGroupTitleLineHeight == key) return cellGroupTheme.titleLineHeight.get(context);
    if (kVanCellGroupInsetPadding == key) return cellGroupTheme.insetPadding.get(context);
    if (kVanCellGroupInsetRadius == key) return cellGroupTheme.insetRadius.get(context);
    if (kVanCellGroupInsetTitlePadding == key) return cellGroupTheme.insetTitlePadding.get(context);
    return null;
  }
}

/// [VanCellGroup]主题数据类
@immutable
class VanCellGroupThemeData with VanProvide {
  final VanCss<VanBgColor> background;
  final VanCss<Color> titleColor;
  final VanCss<EdgeInsets> titlePadding;
  final VanCss<double> titleFontSize;
  final VanCss<double> titleLineHeight;
  final VanCss<EdgeInsets> insetPadding;
  final VanCss<BorderRadius> insetRadius;
  final VanCss<EdgeInsets> insetTitlePadding;

  /// 构造器
  const VanCellGroupThemeData.raw({
    required this.background,
    required this.titleColor,
    required this.titlePadding,
    required this.titleFontSize,
    required this.titleLineHeight,
    required this.insetPadding,
    required this.insetRadius,
    required this.insetTitlePadding,
  });

  /// 拷贝
  VanCellGroupThemeData copyFrom({
    VanCss<VanBgColor>? background,
    VanCss<Color>? titleColor,
    VanCss<EdgeInsets>? titlePadding,
    VanCss<double>? titleFontSize,
    VanCss<double>? titleLineHeight,
    VanCss<EdgeInsets>? insetPadding,
    VanCss<BorderRadius>? insetRadius,
    VanCss<EdgeInsets>? insetTitlePadding,
  }) {
    return VanCellGroupThemeData.raw(
      background: background ?? this.background,
      titleColor: titleColor ?? this.titleColor,
      titlePadding: titlePadding ?? this.titlePadding,
      titleFontSize: titleFontSize ?? this.titleFontSize,
      titleLineHeight: titleLineHeight ?? this.titleLineHeight,
      insetPadding: insetPadding ?? this.insetPadding,
      insetRadius: insetRadius ?? this.insetRadius,
      insetTitlePadding: insetTitlePadding ?? this.insetTitlePadding,
    );
  }

  /// 反序列化
  static VanCellGroupThemeData fromJson(Map<String, dynamic> json) {
    return VanCellGroupThemeData.raw(
      background: VanCss(json[kVanCellGroupBackground.key]),
      titleColor: VanCss(json[kVanCellGroupTitleColor.key]),
      titlePadding: VanCss(json[kVanCellGroupTitlePadding.key]),
      titleFontSize: VanCss(json[kVanCellGroupTitleFontSize.key]),
      titleLineHeight: VanCss(json[kVanCellGroupTitleLineHeight.key]),
      insetPadding: VanCss(json[kVanCellGroupInsetPadding.key]),
      insetRadius: VanCss(json[kVanCellGroupInsetRadius.key]),
      insetTitlePadding: VanCss(json[kVanCellGroupInsetTitlePadding.key]),
    );
  }
}

/// [VanCellGroup]样式
@immutable
class VanCellGroupStyle with VanProvide {
  static VanCellGroupStyle? of(BuildContext context) => VanProvider.of<VanCellGroupStyle>(context);

  final VanCellStyle? cellStyle;
  final VanBgColor? background;
  final Color? titleColor;
  final EdgeInsets? titlePadding;
  final double? titleFontSize;
  final double? titleLineHeight;
  final EdgeInsets? padding;
  final Border? border;
  final BorderRadius? radius;

  bool get isNonNull {
    if (cellStyle == null) return false;
    if (background == null) return false;
    if (titleColor == null) return false;
    if (titlePadding == null) return false;
    if (titleFontSize == null) return false;
    if (titleLineHeight == null) return false;
    if (border == null) return false;
    if (radius == null) return false;
    return true;
  }

  const VanCellGroupStyle({
    this.cellStyle,
    this.background,
    this.titleColor,
    this.titlePadding,
    this.titleFontSize,
    this.titleLineHeight,
    this.padding,
    this.border,
    this.radius,
  });

  VanCellGroupStyle copyFrom({
    VanCellStyle? cellStyle,
    VanBgColor? background,
    Color? titleColor,
    EdgeInsets? titlePadding,
    double? titleFontSize,
    double? titleLineHeight,
    EdgeInsets? padding,
    Border? border,
    BorderRadius? radius,
  }) {
    return VanCellGroupStyle(
      cellStyle: cellStyle ?? this.cellStyle,
      background: background ?? this.background,
      titleColor: titleColor ?? this.titleColor,
      titlePadding: titlePadding ?? this.titlePadding,
      titleFontSize: titleFontSize ?? this.titleFontSize,
      titleLineHeight: titleLineHeight ?? this.titleLineHeight,
      padding: padding ?? this.padding,
      border: border ?? this.border,
      radius: radius ?? this.radius,
    );
  }
}
